'use strict'

var ENV = 'DEV';
const Hapi = require('hapi'); //路由
const Inert = require('inert'); //文件处理
const Sequelize = require('sequelize'); //操作数据库
const fs = require('fs'); //操作数据库

var server = new Hapi.Server();

/*
var sequelize = new Sequelize('app_db','root','',{
    host: 'localhost',
    dialect: 'mysql'
});

sequelize.authenticate()
    .then(function(err){
        console.log('Sql connection has been established successfully.');
    }).catch(function(err){
        console.log('Unable to connect to the database:', err);
});
*/
server.connection({
    port: 3600
});

//server.app.sequelize = sequelize;

// Register webpack HMR, only for non-production environments
if (process.env.NODE_ENV !== 'production') {

    const WebpackConfig = require('./config/webpack.config.js'); // Webpack config
    const HapiWebpackDevMiddleware = require('hapi-webpack-dev-middleware');
    const HapiWebpackHotMiddleware = require('hapi-webpack-hot-middleware');

    server.register([{
        register: HapiWebpackDevMiddleware,
        options: {
            config: WebpackConfig,
            options: {
                noInfo: true,
                publicPath: WebpackConfig.output.publicPath,
                stats: {
                    colors: true
                }
            }
        }
    }, {
        register: HapiWebpackHotMiddleware
    }], function (err) {
        if (err) {
            throw err;
        }
    });

}

server.register([
    Inert,
    require('./main'),
    require('./routes/skintest')
], err => {
    if (err) {
        throw err;
    }

    server.start(err => {
        console.log('Server running at:', server.info.uri);
    });
});